#include <im_memory.h>
#include <check.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_POINTERS (65536)

static IMPointer g_pointers[MAX_POINTERS];


static void memory_setup (void)
{
}

static void memory_teardown (void)
{
}

START_TEST(memory_test_slice_alloc_and_free)
{
	int i = 0;
	
	for (i = 0; i < MAX_POINTERS; i++) {
		g_pointers[i] = im_slice_alloc (i & 0x120);
	}

	for (i = 0; i < MAX_POINTERS; i++) {
		im_slice_free (i & 0x120, g_pointers[i]);
	}
	fprintf (stderr, "slice_alloc_and_free finished!\n");
}
END_TEST

START_TEST(memory_test_malloc_and_free)
{
	int i = 0;
	
	for (i = 0; i < MAX_POINTERS; i++) {
		g_pointers[i] = malloc (i & 0x0130);
	}

	for (i = 0; i < MAX_POINTERS; i++) {
		free (g_pointers[i]);
	}
	fprintf (stderr, "malloc_and_free finished!\n");
}
END_TEST

START_TEST(memory_test_malloc_realloc_and_free)
{
	int i = 0;
	IMPointer p = NULL;

	p = im_malloc(1);
	
	for (i = 0; i < MAX_POINTERS; i++) {
		p = im_realloc (p, i & 0xFFFF);
	}

	im_free (p);
	fprintf (stderr, "malloc_realloc_and_free finished!\n");
}
END_TEST

Suite * memory_suite (void)
{
	Suite *s;
	TCase *tc1, *tc2, *tc3;

	s = suite_create ("memory");
	tc1 = tcase_create ("alloc");
	suite_add_tcase (s, tc1);
	tcase_add_test(tc1, memory_test_slice_alloc_and_free);
	tcase_set_timeout (tc1, 100);
	tcase_add_checked_fixture (tc1, memory_setup, memory_teardown);

	tc2 = tcase_create ("malloc");
	suite_add_tcase (s, tc2);
	tcase_add_test(tc2, memory_test_malloc_and_free);
	tcase_set_timeout (tc2, 100);
	tcase_add_checked_fixture (tc2, memory_setup, memory_teardown);

	tc3 = tcase_create ("realloc");
	suite_add_tcase (s, tc3);
	tcase_add_test(tc3, memory_test_malloc_realloc_and_free);
	tcase_set_timeout (tc3, 100);
	tcase_add_checked_fixture (tc3, memory_setup, memory_teardown);

	
	return s;
}
